import 'package:flutter/material.dart';
import 'package:github_client_app/page/assets/assets_page.dart';
import 'package:github_client_app/page/home/home_page.dart';
import 'package:github_client_app/page/mine/mine_page.dart';
import 'package:github_client_app/page/orders/orders_page.dart';
import 'package:github_client_app/provider/theme_provider.dart';
import 'package:provider/provider.dart';
// import 'package:flutter/cupertino.dart';

class IndexPage extends StatefulWidget {
  const IndexPage({Key? key}) : super(key: key);

  @override
  _IndexPageState createState() => _IndexPageState();
}

class _IndexPageState extends State<IndexPage>
    with SingleTickerProviderStateMixin {
  int _curIndex = 0;
  final PageController _pageController = PageController();
  late final AnimationController _animationController;
  late Animation _scale;
  @override
  initState() {
    super.initState();
    _animationController = AnimationController(
        vsync: this, duration: const Duration(milliseconds: 500))
      ..forward();
    _scale = Tween<double>(begin: 1, end: 24)
        .chain(CurveTween(curve: Curves.bounceOut))
        .animate(_animationController);
  }

  List<BottomNavigationBarItem> _buildBottomNavigationBarItem(theme) {
    return [
      BottomNavigationBarItem(
          icon: Icon(
            Icons.home,
            color: theme.hintColor,
          ),
          activeIcon: Icon(
            Icons.home,
            color: theme.primaryColor,
            size: _scale.value,
          ),
          label: '首页'),
      BottomNavigationBarItem(
          icon: Icon(
            Icons.assessment,
            color: theme.hintColor,
          ),
          activeIcon: Icon(
            Icons.assessment,
            color: theme.primaryColor,
            size: _scale.value,
          ),
          label: '资金'),
      BottomNavigationBarItem(
          icon: Icon(Icons.money_off_csred_sharp, color: theme.hintColor),
          activeIcon: Icon(
            Icons.money_off_csred_sharp,
            color: theme.primaryColor,
            size: _scale.value,
          ),
          label: '订单'),
      BottomNavigationBarItem(
          icon: Icon(Icons.minimize, color: theme.hintColor),
          activeIcon: Icon(
            Icons.minimize,
            color: theme.primaryColor,
            size: _scale.value,
          ),
          label: '我的'),
    ];
  }

  final List<Widget> _pageList = const [
    HomePage(),
    AssetsPage(),
    OrdersPage(),
    MinePage()
  ];

  @override
  Widget build(BuildContext context) {
    print("index page build");
    return Scaffold(
      appBar: AppBar(
        title: const Text('Demo Title'),
      ),
      body: PageView(
        // physics: const NeverScrollableScrollPhysics(), // 禁止滑动
        controller: _pageController,
        onPageChanged: (int index) => {
          setState(() {
            _curIndex = index;
          })
        },
        children: _pageList,
      ),
      bottomNavigationBar: AnimatedBuilder(
        animation: _animationController,
        builder: (context, _) {
          return BottomNavigationBar(
            selectedFontSize: 12,
            type: BottomNavigationBarType.fixed,
            items: _buildBottomNavigationBarItem(Theme.of(context)),
            currentIndex: _curIndex,
            onTap: (value) {
              // _scale.value
              print(_scale.value);
              _animationController.reset();
              _animationController.forward();
              _pageController.jumpToPage(value);
            },
          );
        },
      ),
      // 设置主题
      floatingActionButton: Consumer<ThemeProvider>(
        builder: (context, themeProvider, child) {
          return FloatingActionButton(
            onPressed: () {
              themeProvider.themeMode = 'dark';
            },
            child: const Icon(Icons.color_lens),
          );
        },
      ),
    );
  }
}
